home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / bbsutil / hsrc_117.zip / MAILIN.C < prev    next >
C/C++ Source or Header  |  1990-10-08  |  18KB  |  680 lines

  1. /* Tosser for net/echo mail */
  2.  
  3. #include "msgg.h"
  4. #include "twindow.h"
  5. #include "keys.h"
  6. #include "headedit.h"
  7.  
  8.  
  9. #define MAXKLUDGE 133
  10.  
  11.  
  12. static ulong totalmsgs=0;
  13. static word *anum;           /* Pointer to area # msgs array */
  14. static word pmnum;           /* # messages in packet */
  15. static struct _pkthdr pi;  /* The current incoming packet's header */
  16. static struct _xmsg xmsg;  /* The current msg's header */
  17. static char *text;         /* Pointer to current msg's text body */
  18. static char *pathline;     /* Pointer to current msg's PATH line(s) */
  19. static char *seenby;       /* Pointer to current msg's SEEN-BY line(s) */
  20. static char *area;         /* Pointer to current msg's AREA: tag */
  21. static char *kludge;       /* Pointer to first kludge line in msg */
  22. static char *endptr;       /* End-of-msg pointer */
  23. static char *arcext[]={"MO","TU","WE","TH","FR","SA","SU",NULL};
  24. static char mailerless;
  25.  
  26. char no_strip=0;    /* Don't remove EID's & SEENBYs if > 0 */
  27. char no_fillin=0;   /* Don't complete addresses from kludges if > 0 */
  28.  
  29.  
  30. /* Function declarations */
  31.  
  32. void   pascal import_mail(void);
  33. FILE * pascal open_pkt (word zone,word net,word dest,word d_point,char *domain,word attr,word m_attr);
  34. void   pascal close_pkt (FILE *fp);
  35. static void   pascal close_msg_files(void);
  36. int    pascal write_pkt_msg (FILE *fp,struct _xmsg *amsg,char *text,char *area);
  37. static int    pascal are_we_in_here (char *buf,word net,word node);
  38. static int    pascal crack_pkt (char *pktname);
  39. static int    pascal crack_arc (char *in, char *arcname);
  40. static int    pascal import (void);
  41. static int    pascal move_2_safety (char *pktname);
  42. static int    pascal unknown_msg ();
  43. static int    pascal get_msg (word buflen,char *buffer,FILE *fp);
  44. static void   pascal report(void);
  45.  
  46.  
  47.  
  48.  
  49.  
  50. void pascal import_mail (void) {
  51.  
  52.     register int x;
  53.     word areanum[4096];
  54.     struct ffblk f;
  55.     char s[133];
  56.     char ss[133];
  57.  
  58.     printf("\x1b[2J");
  59.     if(!*inbound) {
  60.         printf("\07I don't know where your Inbound directory is!\n");
  61.         return;
  62.     }
  63.  
  64.     anum=areanum;    /* So it can be accessed globally */
  65.  
  66.     printf("\nImporting mail...");
  67.  
  68.     for(x=0;x<4096;x++) {
  69.         anum[x]=0;
  70.     }
  71.     if(!netboard)netboard=1;
  72.  
  73.     sprintf(s,"%s*.PKT",inbound);
  74.     if(!findfirst(s,&f,0)) {        /* Got naked packet(s) */
  75.         do {
  76.             sprintf(s,"%s%s",inbound,f.ff_name);
  77.             crack_pkt(s);
  78.         } while(!findnext(&f));
  79.     }
  80.  
  81.     x=0;
  82.     while(arcext[x]!=NULL) {
  83.         sprintf(s,"%s*.%s?",inbound,arcext[x]);
  84.         if(!findfirst(s,&f,0)) {        /* Got archived mail */
  85.             do {
  86.                 strcpy(s,inbound);
  87.                 s[strlen(s)-1]=0;
  88.                 if(crack_arc(s,f.ff_name)) continue;
  89.                 else {
  90.                     sprintf(ss,"%s*.PKT",inbound);
  91.                     if(!findfirst(ss,&f,0)) {        /* Got naked packet(s) */
  92.                         unlink(s);
  93.                         do {
  94.                             sprintf(ss,"%s%s",inbound,f.ff_name);
  95.                             crack_pkt(ss);
  96.                         } while(!findnext(&f));
  97.                     }
  98.                 }
  99.  
  100.             } while(!findnext(&f));
  101.         }
  102.         x++;
  103.     }
  104.  
  105.     close_msg_files();    /* All done; pack the bags and go home */
  106.  
  107.     report();
  108. }
  109.  
  110.  
  111.  
  112. void pascal report (void) {
  113.  
  114.     register int x;
  115.  
  116.     printf("\n");
  117.     if(!totalmsgs) {
  118.         printf("No messages imported.\n");
  119.         return;
  120.     }
  121.     for(x=0;x<maxareas;x++) {
  122.         if(anum[x]) {
  123.             printf("Imported %u msgs to area #%u %s.\n",anum[x],marea[x].number,marea[x].name);
  124.         }
  125.     }
  126.     printf("\nTotal msgs imported: %lu\n",totalmsgs);
  127.     if(domail!=2 && domail!=4 && domail!=6) get_char();
  128. }
  129.  
  130.  
  131.  
  132. int pascal crack_arc (char *in,char *arcname) {
  133.  
  134.     char s[256];
  135.  
  136.     sprintf(s,"%s /c UNARC.BAT %s %s",getenv("COMSPEC"),in,arcname);
  137.     do_spawn(s);
  138. }
  139.  
  140.  
  141.  
  142. int pascal crack_pkt (char *pktname) {
  143.  
  144.     register word x;
  145.     FILE *fp;
  146.     long pos;
  147.     long len;
  148.     char *buffer;
  149.     word buflen;
  150.     int  temp;
  151.  
  152.     pmnum=0;
  153.     fp=fopen(pktname,"rb");
  154.     if(!fp) return -1;      /* Return Can't Open */
  155.     fseek(fp,0L,SEEK_END);    /* Get packet size */
  156.     len=ftell(fp);
  157.     if(len<=(long)sizeof(struct _pkthdr)) {
  158.         fclose(fp);
  159.         unlink(pktname);
  160.         printf("\nShort packet discarded.\n");
  161.         return 0;            /* Ignore 'short' packet */
  162.     }
  163.     fseek(fp,0L,SEEK_SET);
  164.  
  165.     if((len-(long)sizeof(struct _pkthdr))<65533L) buflen=(word)((len-(long)sizeof(struct _pkthdr))+1L);
  166.     else buflen=65534;        /* Set minimum buffer length */
  167.     buffer=(char *)malloc(buflen+1);        /* Allocate text buffer */
  168.     if(!buffer) {
  169.         fclose(fp);
  170.         return -2;        /* Return OOM */
  171.     }
  172.  
  173.     fread(&pi,sizeof(struct _pkthdr),1,fp);    /* Get packet header */
  174.  
  175.     printf("\nPacket `%s' is from address %u:%u/%u.%u@%s\n",pktname,pi.orig_zone,pi.orig_net,pi.orig_node,pi.orig_point,pi.domain);
  176.     if(!strcmp(pi.domain,"XBBS")) {
  177.         printf("Mailerless point packet.\n");
  178.         mailerless=1;
  179.     }
  180.     else mailerless=0;
  181.     for(x=0;x<(word)noaddress;x++) {
  182.         if((pi.dest_zone==0 || pi.dest_zone==address[x]->zone) && (pi.dest_net==address[x]->net) && (pi.dest_node==address[x]->node) && (pi.dest_point==0 || pi.dest_point==address[x]->point)) {
  183.             curaddress.zone=address[x]->zone;
  184.             curaddress.net=address[x]->net;
  185.             curaddress.node=address[x]->node;
  186.             curaddress.point=address[x]->point;
  187.             strcpy(curaddress.domain,address[x]->domain);
  188.             break;
  189.         }
  190.     }
  191.     if((pi.dest_zone!=0 && pi.dest_zone!=curaddress.zone) || (pi.dest_net!=curaddress.net) || (pi.dest_node!=curaddress.node) || (pi.dest_point!=0 && pi.dest_point!=curaddress.point)) {
  192.         printf("Packet isn't ours; addressed to %u:%u/%u.%u\n",pi.dest_zone,pi.dest_net,pi.dest_node,pi.dest_point);
  193.         fclose(fp);
  194.         if(buffer)free(buffer);
  195. /*        return(forward_pkt(pktname));    */
  196.         move_2_safety(pktname);
  197.         return 0;
  198.     }
  199.  
  200.     while(!feof(fp)) {
  201.         temp=get_msg(buflen,buffer,fp);
  202.         if(temp==-1) {
  203.             fclose(fp);
  204.             if(buffer)free(buffer);
  205.             move_2_safety(pktname);
  206.             return -3;    /* Return 'minor' error */
  207.         }
  208.         if(temp<0) break;    /* End of packet */
  209.         if(text==NULL || !*text) {
  210.             printf("\nNULL message discarded.\r");
  211.             continue;    /* NULL message */
  212.         }
  213.         if(!temp) {
  214.             for(x=0;x<(word)noaddress;x++) {
  215.                 if(xmsg.dest_net==address[x]->net && xmsg.dest==address[x]->node) {
  216.                     curaddress.zone=address[x]->zone;
  217.                     curaddress.net=address[x]->net;
  218.                     curaddress.node=address[x]->node;
  219.                     curaddress.point=address[x]->point;
  220.                     strcpy(curaddress.domain,address[x]->domain);
  221.                     break;
  222.                 }
  223.             }
  224.             if(xmsg.dest_net==curaddress.net && xmsg.dest==curaddress.node) temp=import();
  225. /*            else forward_msg(buffer);    */
  226.             if(temp) {
  227. /*              unknown_msg();    */
  228.             }
  229.         }
  230.     }
  231.     fclose(fp);         /* Close packet */
  232.     unlink(pktname);     /* Get rid of packet after unpacking */
  233.     if(buffer)free(buffer);
  234.     return 0;            /* Return success */
  235. }
  236.  
  237.  
  238.     static FILE *dataptr=NULL,*textptr=NULL;
  239.  
  240. void pascal close_msg_files (void) {
  241.  
  242.     if(dataptr) fclose(dataptr);
  243.     if(textptr) fclose(textptr);
  244.     dataptr=textptr=NULL;
  245. }
  246.  
  247.  
  248. int pascal import () {  /* Put msg in XBBS base */
  249.  
  250.     register int x;
  251.     static word lastarea=0;
  252.     static word lastmarea;
  253.     word temp=0;
  254.     char textname[133];
  255.     char dataname[133];
  256.  
  257.     if(area==NULL) temp=netboard;
  258.     else {
  259.         for(x=0;x<maxareas;x++) {    /* Find the area tag */
  260.             if(!stricmp(area,marea[x].name)) {
  261.                 temp=marea[x].number;
  262.                 break;
  263.             }
  264.         }
  265.         lastmarea=x;
  266.     }
  267.  
  268.     if(!temp) {
  269.         printf("\nMsg in unknown area \"%s\" ignored.\n",area);
  270.         return 1;        /* Unknown area */
  271.     }
  272.  
  273.     if(temp!=lastarea) {
  274.         if(dataptr)fclose(dataptr);
  275.         if(textptr)fclose(textptr);
  276.         lastarea=temp;
  277.         sprintf(textname,"%sXTEXT.%03x",path,temp);
  278.         sprintf(dataname,"%sXDATA.%03x",path,temp);
  279.         dataptr=fopen(dataname,"a+b");
  280.         textptr=fopen(textname,"a+b");
  281.         if(!dataptr || !textptr) {
  282.             if(dataptr)fclose(dataptr);
  283.             if(textptr)fclose(textptr);
  284.             dataptr=textptr=NULL;
  285.             printf("\nCan't open message base file(s)\n");
  286.             return 2;        /* Error opening message area */
  287.         }
  288.         printf("\n");
  289.     }
  290.  
  291.     if(!area) printf("\x1b[KPkt area NETMAIL (#%u) -=> Msg area %u (#%u)\r",++pmnum,lastarea,++anum[lastmarea]);
  292.     else printf("\x1b[KPkt area %s (#%u) -=> Msg area %u (#%u)\r",area,++pmnum,lastarea,++anum[lastmarea]);
  293.     totalmsgs++;
  294.  
  295.     fseek(dataptr,0L,SEEK_END);
  296.     fseek(textptr,0L,SEEK_END);
  297.  
  298.     xmsg.start=ftell(textptr);
  299.     if((marea[lastma